<?php

include 'ApiConfiguration.php';
include 'Connector.php';

Class MySQL {

    private static $dbRead;

    private  static $dbWrite;

	public static function destruct() {
		if(null !== self::$dbRead) mysql_close(self::$dbRead->getConn());
		if(null !== self::$dbWrite) mysql_close(self::$dbWrite->getConn());
	}

    private static function dbRead() {
		if (null === self::$dbRead || !mysql_ping(self::$dbRead->getConn())) {
            $param = ApiConfiguration::getInstance()->getDataReadDBServer();
            self::$dbRead = new DB_Connector($param['host'], $param['user'], $param['pass'], $param['db_name']);
        }

        return self::$dbRead;
    }
    
    private static function dbWrite() {
        if (null === self::$dbWrite || !mysql_ping(self::$dbWrite->getConn())) {
            $param = ApiConfiguration::getInstance()->getDataWriteDBServer();
            self::$dbWrite = new DB_Connector($param['host'], $param['user'], $param['pass'], $param['db_name']);
        }

        return self::$dbWrite;
    }
	
    /**
     * Execute mysql select queries
     * @param string $query (SELECT statement)
     * @return array
     */
    public static function selectGetArray($query){
        $return = self::dbRead()->select($query);

        if (count($return) > 0) {
            return $return;
        }

        return array();
    }	
	
    /**
     * Execute mysql update queries
     * @param string $query ((UPDATE|INSERT|DELETE) statements)
     * @return array number of affected rows
     */
	public static function update($query) {
		return self::dbWrite()->update($query);
	}

    /**
     * Escape string
     * @param string $string
     * @return string
     */
    public static function api_escape($string){
        return self::dbWrite()->api_escape($string);
    }

	public static function rowsCount($query) {
		$return = self::dbRead()->select($query);

        if ($return) {
			return count($return);
		}

		return 0;
	}

    /**
     * Fetch first row
     * @param string $query
     * @return array
     */
	public static function selectGetRow($query){
        $return = self::dbRead()->select($query);
        
        if (count($return) > 0 ){
            return $return[0];
        }

        return array();
	}

    public static function selectGetField($query, $field){
		$data = self::selectGetRow($query);
        
        if (isset($data[$field])) {
            return $data[$field];
        }

        return false;
	}

    /**
     * Fetch array
     * @param string $query
     * @return array
     */
    public static function selectGetColumn($query, $column, $flag=false){
        $return = self::dbRead()->selectColumn($query, $column, $flag);
        
        if (count($return) > 0) {
            return $return;
        }

        return array();
    }

    public static function masterGetRow($query){
		$return = self::dbWrite()->select($query);

		if (count($return) > 0 ) {
            return $return[0];
		}

		return array();
	}

    public static function masterGetArray($query){
		$return = self::dbWrite()->select($query);

        if (count($return) > 0) {
            return $return;
        }

        return array();
	}
    
    public static function getColumnValueNotation($type, $value) {
        switch ($type) {
            case 'varchar' : return "'" . $value . "'";
            case 'bit' : return "b'" . $value . "'";
        }
    }
}